from django.core.management.base import BaseCommand
from gitlab_integration.models import Project, Environment


class Command(BaseCommand):
    help = '初始化GitLab集成的基础数据'

    def handle(self, *args, **options):
        self.stdout.write('开始初始化GitLab集成基础数据...')

        # 创建默认项目
        projects_data = [
            {'name': '示例项目1', 'code': 'demo1', 'description': '这是一个示例项目'},
            {'name': '示例项目2', 'code': 'demo2', 'description': '这是另一个示例项目'},
            {'name': 'Web应用', 'code': 'webapp', 'description': 'Web应用项目'},
            {'name': '移动应用', 'code': 'mobile', 'description': '移动应用项目'},
            {'name': '数据平台', 'code': 'dataplatform', 'description': '数据平台项目'},
        ]

        for project_data in projects_data:
            project, created = Project.objects.get_or_create(
                code=project_data['code'],
                defaults={
                    'name': project_data['name'],
                    'description': project_data['description']
                }
            )
            if created:
                self.stdout.write(f'创建项目: {project.name}')
            else:
                self.stdout.write(f'项目已存在: {project.name}')

        # 创建默认环境
        environments_data = [
            {'name': '开发环境', 'code': 'dev', 'description': '开发测试环境'},
            {'name': '测试环境', 'code': 'test', 'description': '集成测试环境'},
            {'name': '预发布环境', 'code': 'staging', 'description': '预发布环境'},
            {'name': '生产环境', 'code': 'prod', 'description': '生产环境'},
        ]

        for env_data in environments_data:
            environment, created = Environment.objects.get_or_create(
                code=env_data['code'],
                defaults={
                    'name': env_data['name'],
                    'description': env_data['description']
                }
            )
            if created:
                self.stdout.write(f'创建环境: {environment.name}')
            else:
                self.stdout.write(f'环境已存在: {environment.name}')

        self.stdout.write(self.style.SUCCESS('GitLab集成基础数据初始化完成！'))
